(function(){"use strict";BX.namespace("BX.Landing");BX.Landing.EmbedForms=function(){this.forms=[]};BX.Landing.EmbedForms.formsData={};BX.Landing.EmbedForms.prototype={add:function(e){this.forms.push(new BX.Landing.EmbedFormEntry(e))},remove:function(e){const t=this.getFormByNode(e);if(t){t.unload();this.forms=this.forms.filter((function(e){return e!==t}))}},reload:function(e){this.remove(e);this.add(e)},getFormByNode:function(e){let t=null;this.forms.forEach((function(n){if(e===n.getNode()){t=n;return true}}));return t}};BX.Landing.EmbedFormEntry=function(e){this.node=e;this.formObject=null;this.init()};BX.Landing.EmbedFormEntry.ATTR_FORM_ID="b24form";BX.Landing.EmbedFormEntry.ATTR_FORM_ID_STR="data-b24form";BX.Landing.EmbedFormEntry.ATTR_USE_STYLE="b24formUseStyle";BX.Landing.EmbedFormEntry.ATTR_USE_STYLE_STR="data-b24form-use-style";BX.Landing.EmbedFormEntry.ATTR_DESIGN="b24formDesign";BX.Landing.EmbedFormEntry.ATTR_IS_CONNECTOR="b24formConnector";BX.Landing.EmbedFormEntry.FORM_ID_MATCHER=/^#crmFormInline(\d+)$/i;BX.Landing.EmbedFormEntry.PRIMARY_OPACITY_MATCHER=/--primary([\da-fA-F]{2})/;BX.Landing.EmbedFormEntry.prototype={init:function(){let e=this.node.dataset[BX.Landing.EmbedFormEntry.ATTR_FORM_ID];if(!e){this.showNoFormsMessage();return}e=e.split("|");if(e.length!==1&&e.length!==3){this.showNoFormsMessage();return}this.useStyle=this.node.dataset[BX.Landing.EmbedFormEntry.ATTR_USE_STYLE]==="Y";let t=this.node.dataset[BX.Landing.EmbedFormEntry.ATTR_DESIGN];this.design=t?JSON.parse(t.replaceAll("&quot;",'"')):{};if(e.length===1){if(BX.Landing.getMode()==="view"){return}const t=e[0].match(BX.Landing.EmbedFormEntry.FORM_ID_MATCHER);if(t&&t.length===2){this.loadParamsById(t[1]).then(this.load.bind(this)).catch(this.showNoFormsMessage.bind(this))}else{this.showNoFormsMessage()}}else if(e.length===3){this.id=e[0];this.sec=e[1];this.url=e[2];this.load()}},loadParamsById:function(e){if(!(e in BX.Landing.EmbedForms.formsData)){BX.Landing.EmbedForms.formsData[e]=BX.Landing.Backend.getInstance().action("Form::getById",{formId:e})}return BX.Landing.EmbedForms.formsData[e].then(function(e){if(Object.keys(e).length>0){this.id=e.ID;this.sec=e.SECURITY_CODE;this.url=e.URL}else{return Promise.reject()}}.bind(this))},showNoFormsMessage:function(){if(BX.Landing.getMode()!=="view"){const e=BX.message("LANDING_BLOCK_WEBFORM_NO_FORM");const t=this.node.dataset[BX.Landing.EmbedFormEntry.ATTR_IS_CONNECTOR]&&this.node.dataset[BX.Landing.EmbedFormEntry.ATTR_IS_CONNECTOR]==="Y"?BX.message("LANDING_BLOCK_WEBFORM_NO_FORM_BUS_NEW"):BX.message("LANDING_BLOCK_WEBFORM_NO_FORM_CP");this.node.innerHTML=this.createErrorMessage(BX.message("LANDING_BLOCK_WEBFORM_NO_FORM"),t);BX.onCustomEvent("BX.Landing.BlockAssets.Form:addScript",[{node:this.node,error:e}])}},createErrorMessage:function(e,t){if(BX.Landing.getMode()==="view"){return}if(e===undefined||e===null||!e){e=BX.message("LANDING_BLOCK_WEBFORM_ERROR")}if(t===undefined||t===null||!t){t=BX.message("LANDING_BLOCK_WEBFORM_CONNECT_SUPPORT")}const n='<h2 class="u-form-alert-title"><i class="fa fa-exclamation-triangle g-mr-15"></i>'+e+'</h2><hr class="u-form-alert-divider">'+'<p class="u-form-alert-text">'+t+"</p>";return'<div class="u-form-alert">'+n+"</div>"},load:function(){BX.onCustomEvent("BX.Landing.BlockAssets.Form:addScript",[{success:true,node:this.node,script:this.url}]);this.node.innerHTML="";this.loadScript()},unload:function(){if(typeof b24form==="undefined"||!b24form.App||!this.formObject){return}b24form.App.remove(this.formObject.getId())},getNode:function(){return this.node},setFormObject:function(e){this.formObject=e},loadScript:function(){const e=BX.Landing.getMode()==="edit"||BX.Landing.getMode()==="preview"?100:6e4;const t=Date.now()/e|0;const n=document.createElement("script");n.setAttribute("data-b24-form","inline/"+this.id+"/"+this.sec);n.setAttribute("data-skip-moving","true");n.innerText="(function(w,d,u){"+"var s=d.createElement('script');s.async=true;s.src=u+'?'+("+t+");"+"var h=d.getElementsByTagName('script')[0];h.parentNode.insertBefore(s,h);"+"})(window,document,'"+this.url+"')";this.node.appendChild(n)},onFormLoad:function(e){this.setFormObject(e);if(this.useStyle){this.formObject.adjust(this.getParams())}},getParams:function(){const e={design:{shadow:false,font:"var(--landing-font-family)"}};const t=getComputedStyle(document.documentElement).getPropertyValue("--primary").trim();let n=this.design;for(const e in n.color){if(n.color.hasOwnProperty(e)&&(n.color[e]==="--primary"||n.color[e].match(BX.Landing.EmbedFormEntry.PRIMARY_OPACITY_MATCHER)!==null)){n.color[e]=n.color[e].replace("--primary",t)}}if(n.color.background!==undefined){n.color.popupBackground=n.color.background.length===9?n.color.background.slice(0,7)+"FF":n.color.background}e.design=Object.assign(e.design,n);return e}};const e=new BX.Landing.EmbedForms;window.addEventListener("b24:form:init:before",(function(e){if(BX.Landing.getMode()==="edit"){e.detail.data.editMode=true}}));window.addEventListener("b24:form:init",(function(t){const n=e.getFormByNode(t.detail.object.node.parentElement);if(!!n&&t.detail.object){n.onFormLoad(t.detail.object)}}));BX.addCustomEvent("BX.Landing.Block:init",(function(t){const n=t.block.querySelector(t.makeRelativeSelector(".bitrix24forms"));if(n){e.add(n)}}));BX.addCustomEvent("BX.Landing.Block:remove",(function(t){const n=t.block.querySelector(t.makeRelativeSelector(".bitrix24forms"));if(n){e.remove(n)}}));BX.addCustomEvent("BX.Landing.Block:Node:updateAttr",(function(t){const n=t.block.querySelector(t.makeRelativeSelector(".bitrix24forms"));if(n){e.reload(n)}}))})();
//# sourceMappingURL=form_init.map.js